import com.tfy.dao.BlogMapper;
import com.tfy.pojo.Blog;
import com.tfy.utils.IDUtil;
import com.tfy.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.*;

public class test {

    @Test
    public void test1() {
        SqlSession session = MybatisUtils.getSqlSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        List<Blog> list = mapper.getBlog();
        for (Blog blog : list) {
            System.out.println(blog);
        }
    }

    @Test
    public void addInitBlog() {
        SqlSession session = MybatisUtils.getSqlSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        Blog blog = new Blog();
        blog.setId(IDUtil.genId());
        blog.setTitle("Mybatis如此简单");
        blog.setAuthor("狂神说");
        blog.setCreateTime(new Date());
        blog.setViews(9999);
        mapper.addBlog(blog);
        blog.setId(IDUtil.genId());
        blog.setTitle("Java如此简单");
        mapper.addBlog(blog);
        blog.setId(IDUtil.genId());
        blog.setTitle("Spring如此简单");
        mapper.addBlog(blog);
        blog.setId(IDUtil.genId());
        blog.setTitle("微服务如此简单");
        mapper.addBlog(blog);
        session.commit();//!!!!!!!!!!!!!!!!!注意提交事务
        session.close();
    }

    @Test
    public void testif() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Map map = new HashMap();

        map.put("title", "Java如此简单");
        List<Blog> list = mapper.queryBlogIf(map);
        for (Blog blog : list) {
            System.out.println(blog);
        }
    }

    @Test
    public void testwhere() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Map map = new HashMap();

          map.put("title", "Java如此简单");
         map.put("author","狂神说");
        List<Blog> list = mapper.queryBlogIf2(map);
        for (Blog blog : list) {
            System.out.println(blog);
        }
    }

    @Test
    public void testChoose() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Map map = new HashMap();

//        map.put("title", "Java如此简单");
//        map.put("author","狂神说");
        map.put("views",10);
        List<Blog> list = mapper.queryBlogIf2(map);
        for (Blog blog : list) {
            System.out.println(blog);
        }
    }

    @Test
    public void testSet() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Map map = new HashMap();

        map.put("title", "Java如此简单2");
        map.put("author","狂神说2");
        map.put("id","83b6817161a9490bb5db101e0abffdda");
        mapper.updateBlog(map);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void testQueryBlogForeach(){
        SqlSession session = MybatisUtils.getSqlSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        ids.add(3);
        map.put("ids",ids);
        List<Blog> blogs = mapper.queryBlogForeach(map);
        System.out.println(blogs);
        session.commit();
        session.close();
    }

}
